﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MainMenu
{
    public partial class frm_DienstCFG : Form
    {
        public frm_DienstCFG()
        {
            InitializeComponent();
        }

        private void frm_DienstCFG_Load(object sender, EventArgs e)
        {
            switch(Service_Start_Mode("RM_Neuro_Core", ""))
            {
                case "Auto":
                    this._cb_starttype.SelectedIndex = 0;
                    break;

                case "Manual":
                    this._cb_starttype.SelectedIndex = 1;
                    break;

                default:
                    this._cb_starttype.SelectedIndex = 2;
                    break;
            }
        }

        /// <summary>
        /// Ändetr das Startverhalten eines Dienstes und gibt dieses zurück
        /// </summary>
        /// <param name="serviceName">Name des Dienstes</param>
        /// <param name="startMode">Neuer StartMode oder leer für nur auslesen</param>
        /// <returns>StartMode des Dienstes</returns>
        private string Service_Start_Mode(string serviceName, string startMode)
        {
            uint success = 1;
            
            string filter =
                String.Format("SELECT * FROM Win32_Service WHERE Name = '{0}'", serviceName);

            System.Management.ManagementObjectSearcher query = new System.Management.ManagementObjectSearcher(filter);

            if (query == null)
                return "";

            try
            {
                System.Management.ManagementObjectCollection services = query.Get();

                foreach (System.Management.ManagementObject service in services)
                {
                    if (startMode != "")
                    {
                        System.Management.ManagementBaseObject inParams = service.GetMethodParameters("ChangeStartMode");
                        inParams["startmode"] = startMode;

                        System.Management.ManagementBaseObject outParams = service.InvokeMethod("ChangeStartMode", inParams, null);
                        success = Convert.ToUInt16(outParams.Properties["ReturnValue"].Value);
                    }

                    return service.Properties["StartMode"].Value.ToString();
                }
            }
            catch (Exception)
            {
                return "";
            }

            return "";
        }

        private void _cmd_ok_Click(object sender, EventArgs e)
        {
            string TYP = "Disabled";
            switch(this._cb_starttype.SelectedIndex)
            {
                case 0:
                    TYP = "Auto";
                    break;

                case 1:
                    TYP = "Manual";
                    break;

                default:
                    TYP = "Disabled";
                    break;
            }
            Service_Start_Mode("RM_Neuro_Core", TYP);

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }
    }
}
